$(function(){
	$('#leftMenu-files').addClass('active');
});
$(function() {
	$('.btn-upload').click(function() {
		$('#uploadModal').modal('show');
	});
	$('.btn-createfolder').click(function() {
		$('#createFolderModal').modal('show');
	});
	$('.btn-createfile').click(function(){
		$('#createFileModal').modal('show');
	});
	$('.btn-renameDocument').click(function(){
		$('#nameDocumentRename').val($('#documentNameSelect').val());
		$('#typeDocumentRename').val($('#documentTypeSelect').val());
		$('#idDocumentRename').val($('#documentIdSelect').val());
		$('#oldNameDocumentRename').val($('#documentNameSelect').val());
		$('#renameDocumentModal').modal('show');
	});
	$('.btn-copyDocument').click(function(){
		$('#folderBrowserModal').modal('show');
	});
	$('.btn-share').click(function(){
		shareDocument('');
	});
	$('.btn-moveDocument').click(function(){
		$('#folderBrowserModal').modal('show');
	});
	$("#uploadModal").on("hidden.bs.modal", function() {
		$("#note").html("");
		$("#upload ul").html("");
//		location.reload();
	});
	$('#createFolderModal').on("hidden.bs.modal", function() {
		$("#nameFolderCreate").val("");
	});
	$("#shareFileModal").on("hidden.bs.modal", function() {
		$('#linkShare').val('');
		$('#sendLinkEmail').val('');
		$('#sendLinkMessage').val('');
		$('.copyToClipboard').addClass('hidden');
	});
	$("#shareFolderModal").on("hidden.bs.modal", function() {
		$('#linkShareFolder').val('');
		$('#sendLinkFolderEmail').val('');
		$('#sendLinkFolderMessage').val('');
		$('.copyToClipboard').addClass('hidden');
	});
	$('#createFileModal').on("hidden.bs.modal", function() {
		$('#nameFileCreate').val('');
		$('#contentFileCreate').val('');
	});
	$('.btn-unshareDocument').click(function(){
		$('#confirmUnshareFolderModal').modal('show');
	});
});
$(function() {
	$('.easy-tree').EasyTree();
});
$(document).ready(function() {
	var tableElement;
	var vp_w = $(window).width();
	var vp_h = $(window).height();
	var table;
	if (vp_w >= 768) {
		tableElement = $('#dataTableManagement').dataTable({
			"sDom" : '<"top">rt<"bottom"p><"clear">',
			"aoColumns" : [ {
				"sWidth" : "45%"
			}, {
				"sWidth" : "15%"
			}, {
				"sWidth" : "15%"
			}, {
				"sWidth" : "15%"
			}, {
				"sWidth" : "10%"
			}, {
				"sWidth" : "0%",
				"visible" : false,
				"searchable": true
			},{
				"sWidth" : "0%",
				"visible" : false
			},{
				"sWidth" : "0%",
				"visible" : false
			}  ],
			"iDisplayLength" : 11,
			"bScrollCollapse" : true,
//			"aaSorting": [[ 2, "asc" ]],
			"order": [[ 2, "asc" ]],
			"pagingType" : "full_numbers"
		});
	} else {
		tableElement = $('#dataTableManagement').dataTable({
			"sDom" : '<"top">rt<"bottom"p><"clear">',
			"aoColumns" : [ {
				"sWidth" : "70%"
			}, {
				"sWidth" : "25%",
				"visible" : false
			}, {
				"sWidth" : "15%"
			}, {
				"sWidth" : "15%",
				"visible" : false
			}, {
				"sWidth" : "15%"
			}, {
				"sWidth" : "0%",
				"visible" : false
			},{
				"sWidth" : "0%",
				"visible" : false
			},{
				"sWidth" : "0%",
				"visible" : false
			} ],
			"iDisplayLength" : 11,
			"bScrollCollapse" : true,
//			"aaSorting": [[ 2, "asc" ]],
			"order": [[ 2, "asc" ]],
			"pagingType" : "full_numbers"
		});
	}
	$('#dataTableManagement tbody').on( 'click', 'tr', function () {
		if($(this).find('td:eq(0)').text() != "This folder is empty"){
			if ($(this).hasClass('selected')) {
				$(this).removeClass('selected');
				$(this).find('td:eq(4) button').addClass('hidden-share');
				$('#btn-download').addClass('hidden');
				$('#btn-download').attr('href', '#');
				$('#btn-renameDocument').addClass('hidden');
		    	$('#btn-deleteDocument').addClass('hidden');
		    	$('#btn-moveDocumentToTrash').addClass('hidden');
		    	$('#documentNameSelect').val('');
		    	$('#documentTypeSelect').val('');
		    	$('#documentUrlSelect').val('');
		    	$('#documentIdSelect').val('');
		    	if (! $('#btn-share').hasClass('hidden')) {
		    		$('#btn-share').addClass('hidden');
		    	}
		    }
		    else {
		    	tableElement.$('tr.selected').removeClass('selected');
		    	$(this).addClass('selected');
		    	$('.btn-share-on-tr').addClass('hidden-share');
		    	$(this).find('td:eq(4) button').removeClass('hidden-share');
		    	$('#btn-download').removeClass('hidden');
		    	$('#btn-renameDocument').removeClass('hidden');
		    	$('#btn-deleteDocument').removeClass('hidden');
		    	$('#btn-moveDocumentToTrash').removeClass('hidden');
		    	$('#documentNameSelect').val($(this).find('td:eq(0)').text().trim());
		    	$('#documentUrlSelect').val(tableElement.fnGetData(tableElement.fnGetPosition(this))[6]);
		    	$('#documentTypeSelect').val(tableElement.fnGetData(tableElement.fnGetPosition(this))[5]);
		    	$('#documentIdSelect').val(tableElement.fnGetData(tableElement.fnGetPosition(this))[7]);
		    	var linkDownload;
		    	if ($('#documentTypeSelect').val() === "0") {
		    		$('#btn-share').removeClass('hidden');
		    		linkDownload = "/download/fd/" + tableElement.fnGetData(tableElement.fnGetPosition(this))[6];
		    	}
		    	else{
		    		linkDownload = "/download/f/" + tableElement.fnGetData(tableElement.fnGetPosition(this))[6];
		    	}
		    	$('.btn-download').attr('href', linkDownload);
			}
		}
	});
	
//	$("#addNewRow").on('click', function(){
//		function formatDate(date) {
//			var hours = date.getHours();
//			var minutes = date.getMinutes();
//			minutes = minutes < 10 ? '0'+minutes : minutes;
//			var strTime = hours + ':' + minutes;
//			return date.getFullYear() + "/" + date.getMonth()+1 + "/" + date.getDate() + " " + strTime;
//		}
//		
//		var fileExtension = $('#fileNameForDataTable').val().split('.');
//		tableElement.fnAddData([
//		       "1",
//	           "<a href='#'><img src='/assets/iconManage/Text_File.png'>&nbsp;&nbsp;<span>" + $('#fileNameForDataTable').val() + "</span></a>",
//	           fileExtension[fileExtension.length - 1],
//	           $('#fileSizeForDataTable').val() + " Kb",
//	           formatDate(new Date()),
//	           "<button class='btn btn-success hidden-share' onclick='shareDocument('1$" + $('#fileNameForDataTable').val() + "$" + $('#currentPath').val() + "/')'><i class='fa fa-share'></i> <span>Share</span></button>",
//	           "1"
//	    ]);
//	});
});
$(document).ready(function() {
	callShareHover();
});
function callShareHover(){
	var vp_w = $(window).width();
	var vp_h = $(window).height();
	if (vp_w >= 768) {
		$('.btn-share-on-tr').parent().hover(function() {
			if (!$(this).parent().hasClass('selected')){
				$("button", this).removeClass('hidden-share');
			}
		});
		$('.btn-share-on-tr').parent().mouseleave(function() {
			if (!$(this).parent().hasClass('selected')){
				$("button", this).addClass('hidden-share');
			}
		});
	}
}
var client;
function shareDocument(value){
	//value="type$name$path
	if (value.length > 0){
		var valueArray = value.split("$");
		if (valueArray[0] === "0"){
			var linkShare = $('#contextPath').val() + "/sh/" + valueArray[1];
			$('#shareFolderModal').modal('show');
			$('#linkShareFolder').val(linkShare);
			$('#btnCopyFolderlink').attr('data-clipboard-text',linkShare);
			client = new ZeroClipboard($('#btnCopyFolderlink'), {
				moviePath : "/assets/lib/ZeroClipboard.swf"
			});
		}
		else{
			var linkShare = $('#contextPath').val() + "/s/" + valueArray[1];
			$('#shareFileModal').modal('show');
			$('#linkShare').val(linkShare);
			$('#btnCopyFilelink').attr('data-clipboard-text',linkShare);
			client = new ZeroClipboard($('#btnCopyFilelink'), {
				moviePath : "/assets/lib/ZeroClipboard.swf",
				debug: false
			});
		}
	}
	else{
		if ($('#documentTypeSelect').val() === "0"){
			var linkShare = $('#contextPath').val() + "/sh/" + $('#documentUrlSelect').val();
			$('#linkShareFolder').val(linkShare);
			$('#shareFolderModal').modal('show');
			$('#btnCopyFolderlink').attr('data-clipboard-text',linkShare);
			client = new ZeroClipboard($('#btnCopyFolderlink'), {
				moviePath : "/assets/lib/ZeroClipboard.swf"
			});
		}
		else{
			var linkShare = $('#contextPath').val() + "/s/" + $('#documentUrlSelect').val();
			$('#linkShare').val(linkShare);
			$('#shareFileModal').modal('show');
			$('#btnCopyFilelink').attr('data-clipboard-text',linkShare);
			client = new ZeroClipboard($('#btnCopyFilelink'), {
				moviePath : "/assets/lib/ZeroClipboard.swf",
				debug: false
			});
		}
	}
}
$(document).keyup(function(e) {
	var keynum;
	if (window.event) { // IE                 
		keynum = e.keyCode;
	} else if (e.which) {
		// Netscape/Firefox/Opera                   
		keynum = e.which;
	}
	if (keynum == 46 && $('#documentNameSelect').val() != ""){
		//alert('Delete ' + $('#documentNameSelect').val());
		$('#btn-moveDocumentToTrash').click();
	}
	if (keynum == 113 && $('#documentNameSelect').val() != ""){
//		alert('Rename ' + $('#documentNameSelect').val());
		$('#nameDocumentRename').val($('#documentNameSelect').val());
		$('#typeDocumentRename').val($('#documentTypeSelect').val());
		$('#idDocumentRename').val($('#documentIdSelect').val());
		$('#oldNameDocumentRename').val($('#documentNameSelect').val());
		$('#renameDocumentModal').modal('show');
	}
});
function showMessage(){
	$('.copyToClipboard').removeClass("hidden");
}
function showSetting(url){
	$('#documentValue').val(url);
	$('#sharingSetting').modal('show');
	$('#shareFileModal').modal('hide');
	$('#shareFolderModal').modal('hide');
}
function showModalShare(value){
	var valueArray = value.split("$");
	if (valueArray[0] === "0"){
		$('#linkShareFolder').val(valueArray[1]);
		$('#shareFolderModal').modal('show');
	}
	else{
		$('#linkShare').val(valueArray[1]);
		$('#shareFileModal').modal('show');
	}
}
$(function(){
	$('#peoplePwd').click(function(){
		$('#passwordVisibility').removeClass('hidden');
	});
	$('#anyone').click(function(){
		if (! $('#passwordVisibility').hasClass('hidden')){
			$('#passwordVisibility').addClass('hidden');
		}
	});
	$('#onlyYou').click(function(){
		if (! $('#passwordVisibility').hasClass('hidden')){
			$('#passwordVisibility').addClass('hidden');
		}
	});
});
$(function(){
	$('#renameDocumentForm').bootstrapValidator({
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			nameDocumentRename : {
				validators : {
					notEmpty : {
					}
				}
			}
		}
	});
});
function renameDocument(){
	$("#spinner").spin('modal');
	var nameDocument = $('#nameDocumentRename').val();
	var typeDocument = $('#typeDocumentRename').val();
	var idDocument = $('#idDocumentRename').val();
	var oldNameDocument = $('#oldNameDocumentRename').val();
	var realPath = $('#realCurrentPath').val();
	var userEmailOwner = $('#emailOwner').val();
	if (nameDocument == ""){
		removeSpinner();
		$('#renameDocumentForm .form-group').addClass('has-feedback has-error');
		$('#renameDocumentForm .form-group small').css('display','block');
	}
	jQuery.ajax({
		type : 'POST',
		url : "/manage/renameDocument",
		data : {
			documentName : nameDocument,
			documentType : typeDocument,
			documentId : idDocument,
			documentOldName : oldNameDocument,
			documentPath : realPath,
			documentEmail : userEmailOwner
		},
		async : true,
		dataType : "json",
		success : function(dataReceived) {
			removeSpinner();
			if (dataReceived.info === "Rename file successfully" || dataReceived.info === "Rename folder successfully"){
				$('#renameDocumentModal').modal('hide');
			}
			else{
				$('#renameDocumentForm .form-group').addClass('has-feedback has-error');
				$('#renameDocumentForm .form-group small').css('display','block');
				$('#renameDocumentForm .form-group small').html('Document name already exists');
			}
		}
	});
}